<template>
  <div class="user-list">
    <div class="page-header">
      <h2>用户管理</h2>
      <el-button type="primary" @click="handleAddUser">添加用户</el-button>
    </div>
    
    <el-card class="box-card">
      <div class="search-bar">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="用户名">
            <el-input placeholder="请输入用户名" clearable></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input placeholder="请输入手机号" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary">查询</el-button>
            <el-button>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="120">
        </el-table-column>
        <el-table-column
          prop="nickname"
          label="昵称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          width="120">
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          width="180">
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="注册时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          width="100">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180">
          <template #default="scope">
            <el-button type="text" size="small" @click="handleView(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'

export default {
  setup() {
    const router = useRouter()
    
    return {
      router
    }
  },
  data() {
    return {
      tableData: [
        {
          id: 1001,
          username: 'user1',
          nickname: '张三',
          phone: '13800138001',
          email: 'user1@example.com',
          createdAt: '2023-01-01 12:00:00',
          status: 1
        },
        {
          id: 1002,
          username: 'user2',
          nickname: '李四',
          phone: '13800138002',
          email: 'user2@example.com',
          createdAt: '2023-01-02 14:30:00',
          status: 1
        },
        {
          id: 1003,
          username: 'user3',
          nickname: '王五',
          phone: '13800138003',
          email: 'user3@example.com',
          createdAt: '2023-01-03 09:15:00',
          status: 0
        }
      ],
      total: 3
    }
  },
  methods: {
    handleAddUser() {
      // 跳转到添加用户页面
      this.router.push('/admin/user/add');
    },
    handleView(row) {
      // 跳转到用户详情页面
      this.router.push(`/admin/user/${row.id}`);
    },
    handleEdit(row) {
      // 跳转到用户编辑页面
      this.router.push(`/admin/user/edit/${row.id}`);
    },
    handleDelete(row) {
      // 弹出确认删除对话框
      this.$confirm('确认删除该用户吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里添加删除用户的逻辑
        // 例如：过滤掉当前行
        this.tableData = this.tableData.filter(item => item.id !== row.id);
        this.total -= 1;
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>
.user-list {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-bar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 